//
//  LXDelayRecordViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/22.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXDelayRecordViewController.h"
#import "LXStatisticsListTableViewCell.h"
#import "LXDelayModel.h"
#import "LXDelayApplyViewController.h"
@interface LXDelayRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger page;

@end

@implementation LXDelayRecordViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    
    if (!ObjectIsEmpty(self.carModel)) {
        
        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 70, KStatuesBarHeight + 5, 60, 40)];
       rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
       [rightButton setTitle:@"新增" forState:UIControlStateNormal];
        [rightButton setImage:Image(@"icon_add") forState:UIControlStateNormal];
       [rightButton setTitleColor:mainColor forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:rightButton];
    }
    
    if (self.type == 1) {
        [self layoutCustomNav:self.view title:@"延期记录"];
    }
    else {
        [self layoutCustomNav:self.view title:@"延期提醒"];
    }
    
    
    

    [self.view addSubview:self.tableView];
    
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];

    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.page++;
        [self loadData];
    }];
    
 
    
}

- (void)loadData{

  
    
    NSMutableDictionary *dic = @{
        @"rows":@(20),
        @"page":@(self.page)
    }.mutableCopy;

    
    if (!StringIsEmpty(self.carModel.ID)) {
        [dic setValue:self.carModel.ID forKey:@"carId"];
    }
    
    if (!StringIsEmpty(self.customerId)) {
        [dic setValue:self.customerId forKey:@"customerId"];
    }
    
    [MSNetworkManager getRevertUrl:GET_GetTsigncontractdelay params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        
        if (self.page==1) {
            [self.data removeAllObjects];
        }
        NSInteger totalPage = [resonseObj[@"total"] integerValue];
            
        NSArray *records = resonseObj[@"records"];
        
        [self.data addObjectsFromArray:[LXDelayModel  mj_objectArrayWithKeyValuesArray:records]];
        
        
        if (self.data.count>=totalPage) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
        else {
            self.tableView.mj_footer.state = MJRefreshStateIdle;
        }
        
            
        [self.tableView reloadData];
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}



- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"cell3";
    LXStatisticsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXStatisticsListTableViewCell class]) owner:self options:nil] objectAtIndex:2];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
    }
    LXDelayModel *model = self.data[indexPath.row];
    cell.delayModel = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
//    LXDelayModel *model = self.data[indexPath.row];
//
//
//    NSDictionary *dic = @{
//        @"id":model.ID,
//    };
//
//    [MSNetworkManager getRevertUrl:GET_GetTsigncontractdelayDetail params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
//
//
//
//        LXDelayModel *model = [LXDelayModel  mj_objectWithKeyValues:resonseObj];
//        LXDelayApplyViewController *vc = [[LXDelayApplyViewController alloc] init];
//        vc.type = 2;
//        vc.delayModel = model;
//        [self pushToNextController:vc];
//
//
//    } failure:^(NSUInteger code, NSString * _Nonnull message) {
//        [self endRefreshing];
//        [self isHaveEmptyData];
//    } withShowHud:YES];

  
}





- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, kScreenWidth, kScreenHeight - kViewTopHeight - 5) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = ColorWithF7F8FA;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_header.state = MJRefreshStateIdle;
        [self.tableView.mj_footer resetNoMoreData];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}


-(void)clickBtn{
    LXDelayApplyViewController *vc = [[LXDelayApplyViewController alloc] init];
    vc.type = 1;
    LXDelayModel *model = [LXDelayModel new];
    model.carNumber = self.carModel.carNum;
    model.carId = self.carModel.ID;
    model.customerName = self.carModel.customerName;
    model.customerId = self.carModel.driverId;
    vc.delayModel = model;
    [self pushToNextController:vc];
    
}

@end
